* Elderly outcomes in critical care DO file *

import excel "/Users/benmorton/Documents/Ben's documents/Research/Projects/Elderly outcomes/Elderly for analysis 2019 GDV3.xls", sheet("Sheet1") firstrow
set more off

* Label variables *

label variable Episode_No "Episode number"
label variable Gender "Gender"
label variable Age "Patient age at admission"
label variable Hosp_ad "Hospital admission date"
label variable Crit_ad "Critical care admission date"
label variable Year_ad "Year of admission"
label variable Hosp_crit "Days between hospital and critical care admission"
label variable Hosp_crit_48 "More than 48 hours between hospital and critical care admission"
label variable LOS "Length of stay"
label variable Readmission "Critical care readmission"
label variable Readmission_within_hosp_ad "Readmission to critical care within the index hospital admission"
label variable Diagnosis "Patient critical care diagnosis"
label variable Med_Surg_Diag "Medical or surgical diagnosis"
label variable Parent_speciality "Parent specialty at admission"
label variable APACHE2 "APACHE 2 Score"
label variable Age85plus "Age 85 or more"
label variable pHless7quarter "pH less than 7.25"
label variable pHless7tenth "pH less than 7.1"
label variable Lactate "Patient lactate"
label variable Hosp_Mort "Hospital Mortality"
label variable Sabadell "Sabadell score"
label variable Final_review "Date of final review"
label variable ITU_Final_days "Days from ITU admission to the final review date"
label variable Date_death "Date of death"
label variable ITU_death_days "Days from ITU admission to death"
label variable Days_alive "Days alive from ITU admission"
label variable SOFA "SOFA Score"
label variable Osteoporosis "Osteoporosis"
label variable Arthritis "Osteoarthritis"
label variable COPD "Asthma or COPD"
label variable Angina "Angina"
label variable Prev_MI "Previous myocardial infarction"
label variable CCF "Congestive cardiac failure"
label variable Stroke "Stroke or TIA"
label variable Diabetes "Diabetes mellitus"
label variable Neurological_disease "Neurological disease"
label variable PVD "Peripheral vascular disease"
label variable Depression "Anxiety or depression"
label variable obesity "Obesity"
label variable Ulcer "Gastric or duodenal ulcer, hernia or reflux"
label variable Functional_Comorbidity_Score "Functional comorbidity score"
label variable CKD "Chronic Kidney Disease"
label variable Chronic_liver_disease "Chronic Liver Disease"
label variable Total_comorbidity_score "Total co-morbidity score"
label variable Lowest_pH "Lowest pH"
label variable Lowest_platelet "Lowest platelet count"
label variable Lowest_SBP "Lowest systolic blood pressure"
label variable Lowest_GCS "Lowest GCS"
label variable Highest_creatinine "Highest creatinine"
label variable Highest_bilirubin "Highest bilirubin"
label variable FiO2 "FiO2 for lowest PaO2"
label variable PaO2 "Lowest PaO2 (kPa)"
label variable PaO2_mmHg "Lowest PaO2 (mmHg)"
label variable PFratio "Lowest P/F ratio"
label variable PT "Prothrombin time"
label variable LSOA "Lower Layer Super Ouput Area"
label variable IMDR "Index of Multiple Deprivation Rank"
label variable IMDR_10 "Index of Multiple Deprivation Rank Decile"
label variable IDAOPI "IDAOPI Rank"
label variable IDAOPI_10 "IDAOPI Decile"
label variable Time_alive_years "Time Alive in years"
label variable AMSS "Modified SOFA"
label variable CRFCS "Cardiorespiratory functional comorbidity"
label variable ICU_Mort "ICU Mortality"

drop if Gender ==.
			 
* Label values *

label define gender 1 "male" 2 "female"
label values Gender gender

label define YesNo 1 "Yes" 0 "No"
label values Hosp_crit_48 Readmission Age85plus pHless7tenth ICU_Mort YesNo
label values Osteoporosis Arthritis COPD Angina Prev_MI CCF Diabetes Neurological_disease PVD Depression obesity Ulcer CKD Chronic_liver_disease YesNo

label define diag 1 "medical" 2 "surgical"
label values Med_Surg_Diag diag

label define death 1 "died" 0 "alive"
label values Hosp_Mort ICU_Mort death

* Brief descriptives *

tabulate ICU_Mort
tabulate Hosp_Mort
tab Age
tab Gender

tab CRFCS
gen CRFCS_Abridged =.
recode CRFCS_Abridged .=0 if CRFCS ==0
recode CRFCS_Abridged .=1 if CRFCS ==1
recode CRFCS_Abridged .=2 if CRFCS ==2
recode CRFCS_Abridged .=2 if CRFCS ==3
recode CRFCS_Abridged .=2 if CRFCS ==4
tab CRFCS_Abridged

tab Functional_Comorbidity_Score
gen FCS =.
recode FCS .=0 if Functional_Comorbidity_Score ==0
recode FCS .=1 if Functional_Comorbidity_Score ==1
recode FCS .=2 if Functional_Comorbidity_Score ==2
recode FCS .=3 if Functional_Comorbidity_Score ==3
recode FCS .=4 if Functional_Comorbidity_Score ==4
recode FCS .=4 if Functional_Comorbidity_Score ==5
recode FCS .=4 if Functional_Comorbidity_Score ==6
recode FCS .=4 if Functional_Comorbidity_Score ==7
recode FCS .=4 if Functional_Comorbidity_Score ==8
recode FCS .=4 if Functional_Comorbidity_Score ==9
tab FCS


recode Lowest_pH (min/7.0499=1) (7.05/7.14999=2) (7.15/7.24999=3) (7.25/7.3499=4) (7.35/max=5), gen(pHcategory)
label define pH 1 "<7.05" 2 "7.05-7.15" 3 "7.15-7.25" 4 "7.25-7.35" 5 ">7.35"
label values pHcategory pH
tab pHcategory

recode Lactate (min/3.9999=1) (4/5.9999=2) (6/7.999=3) (8/max=4), gen(Lactatecategory)
label define lac 1 "0-4" 2 "4-6" 3 "6-8" 4 ">8"
label values Lactatecategory lac
tab Lactatecategory

recode Lowest_SBP (min/69.999=1) (70/79.9999=2) (80/89.9999=3) (90/max=4), gen(SBPcat)
label define sbp 1 "<70" 2 "<80" 3 "<90" 4 ">90"
label values SBPcat sbp
tab SBPcat

gen GCSbin =.
recode GCSbin .=0 if Lowest_GCS==8 | Lowest_GCS<8
recode GCSbin .=1 if Lowest_GCS>8
label define gcs 1 "GCS 9 or more" 0 "GCS 8 or less"
label values GCSbin gcs
tab GCSbin

// General demographics

summ Age, detail
tab Gender
tab Readmission
tab Readmission_within_hosp_ad
tab Diagnosis
tab Med_Surg_Diag
summ APACHE2, detail
tab Hosp_Mort
summ ITU_death_days, detail
summ LOS, detail
summ FCS, detail
summ IMDR_10, detail

// Demographics by medical/surgical diagnosis

by Med_Surg_Diag, sort: tab Gender
by Med_Surg_Diag, sort: tab Readmission
by Med_Surg_Diag, sort: tab Readmission_within_hosp_ad
by Med_Surg_Diag, sort: tab Hosp_Mort
by Med_Surg_Diag, sort: tab ICU_Mort

tab Med_Surg_Diag Gender, chi2 row
tab Med_Surg_Diag Readmission, chi2 row
tab Med_Surg_Diag Readmission_within_hosp_ad, chi2 row
tab Med_Surg_Diag Hosp_Mort, chi2 row
tab Med_Surg_Diag ICU_Mort, chi2 row
tab Med_Surg_Diag GCSbin, chi2 row

by Med_Surg_Diag, sort: summ ITU_death_days, detail
by Med_Surg_Diag, sort: summ LOS, detail
by Med_Surg_Diag, sort: summ FCS, detail
by Med_Surg_Diag, sort: summ IMDR_10, detail
by Med_Surg_Diag, sort: summ Age, detail
by Med_Surg_Diag, sort: summ APACHE2, detail

ranksum ITU_death_days, by(Med_Surg_Diag)
ranksum LOS, by(Med_Surg_Diag)
ranksum FCS, by(Med_Surg_Diag)
ranksum IMDR_10, by(Med_Surg_Diag)
ranksum Age, by(Med_Surg_Diag)
ranksum APACHE2, by(Med_Surg_Diag)


// Time to death

recode ITU_death_days (min/30=1) (30.01/90=2) (90.01/183=3) (183.01/365=4) (365.01/730=5) (730.01/1095=6) (1095.01/1460=7) (1460.01/1825=8), gen(time_to_event)
replace time_to_event = 9 if time_to_event >1825
label define tte 1 "30 day mortality" 2 "90 day mortality" 3 "Six month mortality" 4 "One year mortality" 5 "Two year mortality" 6 "Three year mortality" 7 "Four year mortality" 8 "Five year mortality" 9 "Alive or death after five years"
label values time_to_event tte
tab time_to_event


// Time to death by medical surgical diagnosis

by Med_Surg_Diag, sort: tab time_to_event


// Time to death by illness severity marker

tabulate time_to_event SBPcat 
tabulate time_to_event Lactatecategory
tabulate time_to_event pHcategory

// Hospital mortality by illness severity marker

tabulate Hosp_Mort SBPcat 
tabulate Hosp_Mort Lactatecategory
tabulate Hosp_Mort pHcategory

// Length of stay by illness severity marker

by SBPcat, sort: summ LOS, detail
by Lactatecategory, sort: summ LOS, detail
by pHcategory, sort: summ LOS, detail

